package com.cslc.wechat.util;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * 数据库连接池操作工具类，tomcat连接池会自动回收资源，所以不需要手动释放资源
 * @author kll
 *
 */
public class JDBCUtil {
	
	private static DataSource ds = null;
	private static Context initContext = null;
	private static Context envContext = null;
	
	static {
		try {
			initContext = new InitialContext();
			envContext = (Context) initContext.lookup("java:/comp/env");
			ds = (DataSource) envContext.lookup("jdbc/test");
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	/**
	 * 从数据源中获取连接
	 * @return
	 * @throws NamingException
	 * @throws SQLException
	 */
	@SuppressWarnings("finally")
	public static Connection getConnection() {
		Connection conn = null;
		try {
			conn = ds.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			return conn;
		}
	}
}
